<template>
	<u-popup :show.sync="showPopup" :round="10" mode="bottom" @close="close">
		<view class="popup_content">
			<view class="top_box">
				<view @click="addAndEditBtn()" class="btn">
					新增
				</view>
				<view class="ipt_box">
					<u-icon name="search" color="#c0c0c0" size="28"></u-icon>
					<input v-model="pageInfo.cate_name" @input="searchCategory()" placeholder="关键字查找类别" type="text" />
				</view>

				<view @click="close()" class="text_box">
					关闭
				</view>
			</view>
			<view v-if="categoryList.length == 0" class="empty_box">
				<u-empty text="暂无分类"></u-empty>
			</view>
			<!-- 分类列表-->
			<view v-else class="stashList_box">
				<view @click.stop="addAndEditBtn(item)" class="stashItem_box" v-for="(item,index) in categoryList"
					:key="item.cate_id">
					<view class="left">
						<u-icon name="grid" color="#1296db" size="40"></u-icon>
						<text>{{item.cate_name}}</text>
					</view>
					<view style="display: flex;">
						<view style="margin-right: 20rpx;" @click.stop="deleteCategory(item.cate_id)" class="right">
							删除
						</view>
						<view @click.stop="selectCategory(item)" class="right">
							选择
						</view>
					</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>
<script>
	import {
		deleteCategoryApi
	} from '@/api/categoryManage.js'
	export default {
		data() {
			return {
				// categoryList: [],
				pageInfo: {
					page: 1,
					pageSize: 100,
					cate_name: ""
				},
				timer: null
			}
		},
		props: {
			showPopup: {
				type: Boolean,
				default: false
			},
			categoryList: {
				type: Array,
				default: () => []
			}
		},
		mounted() {},
		methods: {

			close() {
				this.$emit('update:showPopup', false)
				this.pageInfo.cate_name = ''
			},
			//选择类别
			selectCategory(item) {
				this.$parent.formData.cate_id = item.cate_id
				this.$parent.cate_name = item.cate_name
				this.close()
			},
			//搜索类别
			searchCategory() {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.$parent.getCategoryList(this.pageInfo)
				}, 500)
			},
			//新增类别
			addAndEditBtn(item) {
				if (item) {
					delete item.create_by
					delete item.create_time
					delete item.org_id
					delete item.update_by
					delete item.update_time
					const res = JSON.stringify(item)
					uni.navigateTo({
						url: `/commonToolPages/newCategory/newCategory?brandInfo=${res}`
					})
				} else {
					uni.navigateTo({
						url: `/commonToolPages/newCategory/newCategory`
					})
				}
			},
			//删除类别
			async deleteCategory(cate_id) {

				const that = this
				uni.showModal({
					content: '确定要删除此分类吗',
					success: async function(res) {
						if (res.confirm) {
							await deleteCategoryApi({
								cate_id: cate_id
							})
							that.$parent.getCategoryList()

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});



			}
			// //获取分类列表
			// async getCategoryList() {
			// 	const {
			// 		data: {
			// 			data: {
			// 				list
			// 			}
			// 		}
			// 	} = await getCategoryListApi(this.pageInfo)
			// 	this.categoryList = list
			// }
		},
	}
</script>

<style lang="scss" scoped>
	.popup_content {
		// height: 80vh;

		.top_box {
			display: flex;
			justify-content: space-between;
			font-size: 26rpx;
			padding: 20rpx;

			.btn {
				width: 120rpx;
				height: 70rpx;
				background-color: #39cccc;
				color: #fff;
				text-align: center;
				line-height: 70rpx;
				border-radius: 35rpx;

			}

			.ipt_box {
				display: flex;
				align-items: center;
				width: 480rpx;
				height: 70rpx;
				padding: 0 20rpx;
				background-color: #f2f2f2;
				border-radius: 35rpx;
				box-sizing: border-box;

				input {
					margin-left: 20rpx;
				}
			}

			.text_box {
				color: #60aeff;
				line-height: 70rpx;
			}
		}

		.stashList_box {
			height: 1000rpx;
			overflow: auto;
		}

		.stashItem_box {
			border-bottom: 1px solid #f2f2f2;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;

			.left {
				display: flex;
				align-items: center;
				font-weight: 700;

				text {
					margin-left: 20rpx;
				}
			}

			.right {
				width: 90rpx;
				height: 50rpx;
				background-color: #39cccc;
				color: #fff;
				text-align: center;
				line-height: 50rpx;
				border-radius: 35rpx;
				font-size: 24rpx;
			}
		}
	}

	.empty_box {
		padding-top: 200rpx;
		height: 1000rpx;
		box-sizing: border-box;
	}
</style>